//
//  ContentModeViewController.swift
//  iPhone
//
//  Created by Toj on 6/12/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class ContentModeViewController: UIViewController {
    var topInset = CGFloat.nav_barFullHeight
    
    private let viewModel = ContentModeViewModel()
    
    /* collectionView */
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        let minimumLineSpacing: CGFloat = 10
        let minimumInteritemSpacing: CGFloat = 10
        layout.minimumLineSpacing = minimumLineSpacing
        layout.minimumInteritemSpacing = minimumInteritemSpacing
        let iwidth = CGFloat.sr_width - minimumLineSpacing * 2
        let iheight = iwidth * 9 / 16.0
        let itemSize = CGSize(width: iwidth, height: iheight)
        layout.itemSize = itemSize
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        
        view.showsVerticalScrollIndicator = false
        view.backgroundColor = .white
        
        view.contentInset = UIEdgeInsets(top: topInset, left: 0, bottom: 20, right: 0)
        view.contentInsetAdjustmentBehavior = .never
        
        view.register(UIContentModeCell.cell_class, forCellWithReuseIdentifier: UIContentModeCell.cell_identifier)
        
        view.dataSource = self
        
        return view
    }()
}

// MARK: - 生命周期
extension ContentModeViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - UICollectionViewDataSource
extension ContentModeViewController: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.cellModels.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifierClass: UIContentModeCell.self, for: indexPath)
        cell.cellModel = viewModel.cellModels[indexPath.row]
        return cell
    }
}

// MARK: - 初始化设置
private extension ContentModeViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(collectionView)
    }
    
    func setupMakeLayoutSubviews() {
        collectionView.snp.makeConstraints { make in
            make.edges.equalTo(view)
        }
    }
    
    func setupMakeInitialize() {
        viewModel.setupCellModels()
        collectionView.reloadData()
    }
}
